//
//  WorldWideTableView.m
//  Sth_On_The_Road
//
//  Created by LEO on 12/30/15.
//  Copyright © 2015 Leo_Lei. All rights reserved.
//

#import "WorldWideTableView.h"
#import "ArticleCell.h"
#import "ArticleDetailController.h"
#import "ArticleModel.h"
#import "RecommendView.h"

@interface WorldWideTableView(){
    //  推荐视图
    RecommendView *_recommendView;
    UIView *_headerView;
    UIScrollView *_scrollView;
}

@property (nonatomic, strong) ArticleModel *articleModel;

@end

@implementation WorldWideTableView

#pragma mark - 添加表视图
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        //  设置代理、数据源对象
        self.delegate = self;
        self.dataSource = self;
        
        //  注册单元格
        UINib *nib = [UINib nibWithNibName:@"ArticleCell" bundle:nil];
        [self registerNib:nib forCellReuseIdentifier:ARTICLECELLID];
        
        //  去掉分割线样式
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self createHeaderView];
    }
    return self;
}

#pragma mark - 添加表视图的头视图
- (void)createHeaderView {
    //  1、创建父视图
    CGRect headerFrame = CGRectMake(0, 0, kScreenWidth, TABBARHEIGHT);
    _headerView = [[UIView alloc] initWithFrame:headerFrame];
    
    //  2、加载xib创建推荐视图
    CGRect recommendFrame = headerFrame;
    _recommendView = [[NSBundle mainBundle] loadNibNamed:@"RecommendView"
                                                   owner:self
                                                 options:nil].lastObject;
    _recommendView.frame = recommendFrame;
    [_headerView addSubview:_recommendView];
    
    self.tableHeaderView = _headerView;
}

#pragma mark - 表视图的代理、数据源方法
#pragma mark 表视图的组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark 表视图单组中的单元格数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

#pragma mark 单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

#pragma mark 单元格内容填充
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //  创建单元格
    ArticleCell *articleCell = [tableView dequeueReusableCellWithIdentifier:ARTICLECELLID
                                                               forIndexPath:indexPath];
    articleCell.articleModel = _dataArray[indexPath.row];
    articleCell.backgroundColor = kBackgroundColor;
    return articleCell;
}

#pragma mark - 访问文章详情页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //  文章详情页面
    ArticleDetailController *articleDetail = [[ArticleDetailController alloc] init];
    
    //  当页面被push到下一页面时, 隐藏标签栏
    articleDetail.hidesBottomBarWhenPushed = YES;
    articleDetail.articleModel = _dataArray[indexPath.row];
    [self.belongController.navigationController pushViewController:articleDetail animated:YES];
    
    //  取消单元格选中样式
    [self deselectRowAtIndexPath:indexPath animated:YES];
}

@end
